package com.jk.vehicle.mini.controller.base;

import com.alibaba.excel.util.CollectionUtils;
import com.jk.vehicle.mini.dto.UserDTO;
import com.jk.vehicle.mini.entity.User;
import com.jk.vehicle.mini.entity.UserBicycleLink;
import com.jk.vehicle.mini.service.CorpService;
import com.jk.vehicle.mini.service.UserBicycleLinkService;
import com.jk.vehicle.mini.service.UserService;
import com.jk.vehicle.core.controller.BaseController;
import com.jk.vehicle.core.entity.ResponseEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jk.vehicle.mini.util.BeanMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * This is Description for UserController
 *
 * @author Administrator
 * @date 2021/02/08
 */
@Api(value = "用户单表接口", tags = "用户单表接口")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

	/**
	 * UserService服务
	 */
    @Resource
    private UserService userService;

    @Resource
    private CorpService corpService;

    @Resource
    private UserBicycleLinkService userBicycleLinkService;

	/**
	 * 更新user√
	 * @param user
	 * @return
	 */
	@ApiOperation("更新登录用户信息")
    @PutMapping
    public ResponseEntity update(@RequestBody User user) {
        user = userService.findById(user.getId(), null);
        if (user == null) {
            return failResult("该用户不存在！");
        }
        user.setUpdateDate(new Date());
        List<String> dsNameList = new ArrayList<>();
        List<UserBicycleLink> userBicycleLinkList = userBicycleLinkService.queryUserBicycleLinkCorpById(user.getId());
        if (!CollectionUtils.isEmpty(userBicycleLinkList)) {
            for (UserBicycleLink userBicycleLink: userBicycleLinkList) {
                String dsName = corpService.selectDsNameById(userBicycleLink.getCorpId());
                dsNameList.add(null);// null：代表通用数据源，非空代表独立数据源
                dsNameList.add(dsName);
            }
        }
        //如果当前用户没有绑定车辆只需要更新通用数据源的用户信息
        if (!CollectionUtils.isEmpty(userBicycleLinkList)) {
            for (String dbName: dsNameList) {
                userService.update(user, dbName);
            }
        } else {
            userService.update(user, null);
        }
        return successResult(user);
    }

	/**
	 * user详情√
	 * @param id
	 * @return
	 */
	@ApiOperation("查询用户详情")
    @GetMapping("/{id}/{corpId}")
    public ResponseEntity detail(@PathVariable Long id, @PathVariable Long corpId) {
        if (corpId == null || corpId.longValue() == 0) {
            return failResult("厂家id不能为空！");
        }
        User user = userService.findById(id, null);
        if (user == null) {
            return failResult("该用户不存在！");
        }
        user = userService.findById(id, null);
        return successResult(user);
    }
}
